
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Xml;


using Swaf.BizObj;
using Swaf.Resource;

namespace Swaf.BizObj.fieldValidator
{
	/// <summary> The concrete FieldValidator derived class that uses regular expressions.
	/// </summary>
	[Serializable]
	public class PatternValidator : FieldValidator
	{
		public override string validateNoThrow(IBizObj bizObj, string name, string newValue)
		{
			Match m = Application.currentApp.resMgr.matches(myRegexp_, newValue);
			if (m.Success)
				return null;
			// Success: the pattern it matched
			return "Must be " + description_ + ": \"" + newValue + "\"";
		}
		
		protected internal override FieldValidator create(string context, string a, string b, string c)
		{
			if (b == null || b.Length == 0)
				return new PatternValidator(context, myPattern_, description_);
			string details = Application.currentApp.resMgr.patternErrorMessage(b);
			if (details != null)
				parseError(details + ": " + context);
			return new PatternValidator(context, b, "consistent with pattern " + a + b + a);
		}
		
		/// <summary>Constructs a PatternValidator using the <em>(known good!!)</em> pattern and description.
		/// </summary>
		/// <param name="pattern">The pattern.  <em>Must</em> be a valid pattern (else any errors in the
		/// pattern will be logged to the log file, but the <c>PatternValidator</c> object will
		/// continue to exist!).
		/// 
		/// </param>
		public PatternValidator(string context, string pattern, string description):base(context)
		{
			myPattern_ = pattern;
			myRegexp_ = Application.currentApp.resMgr.knownGoodPattern(pattern);
			description_ = description;
		}
		
		/// <summary>This is the Pattern associated with 'this' validator.
		/// </summary>
		protected internal string myPattern_;
		
		/// <summary>This is the compiled regular expression object associated with 'this' validator.
		/// </summary>
		protected internal Regex myRegexp_;
		
		/// <summary>This is the human-readable description of what the validator is expecting.
		/// E.g., "an identifier", "all uppercase letters", etc.
		/// </summary>
		protected internal string description_;
		
		public override string details(string prefix)
		{
			return prefix + "PatternValidator(\"" + this + "\") { pattern=/" + myPattern_ + "/, patternRE=/" + myRegexp_.ToString() + "/, description=\"" + description_ + "\" }";
		}
		
	}
}